package com.example.demo.Controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.Mapper.BlogMapper;
import com.example.demo.Mapper.GsdtMapper;
import com.example.demo.Service.BlogService;
import com.example.demo.Service.BlogServiceImpl;
import com.example.demo.Service.GsdtServiceImpl;
import com.example.demo.pojo.Blog;
import com.example.demo.pojo.Gsdt;
import com.example.demo.utils.MarkdownUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Random;

@Controller
public class BlogController {

    @Autowired
    private BlogServiceImpl mapper;
    @Autowired
    private GsdtMapper gsdtMapper;
    @Autowired
    private GsdtServiceImpl gsdtService;
    @PostMapping("/admin/blogInputs")
    public String SaveorEditBlog(Blog blog, RedirectAttributes redirect){
        if(blog.getId() == null){
            blog.setId(new Random().nextInt(9999999)+1);
            mapper.InsertBlog(blog);
        }else{
            mapper.UpdateBlog(blog.getId(),blog);
        }
        return "redirect:/admin/blogs";
    }
    @RequestMapping("/blog/{id}")
    public String Blogs(@PathVariable int id, Model model, RedirectAttributes redirectAttributes){
        Blog blog = mapper.selectBlogById(id);
        if (blog == null){
            return "redirectAttributes:/";
        }
        Blog b = new Blog();
        BeanUtils.copyProperties(blog,b);
        String content = b.getContent();
        blog.setContent(MarkdownUtils.markdownToHtmlExtensions(content));
        model.addAttribute("blog", blog);
        return "blog";
    }
    @GetMapping("/admin/inputs")
    public String inputs(Model model){
        model.addAttribute("blog",new Blog());
        return "admin/blogs-inputs";
    }
    @RequestMapping("/admin/blogs/{id}/input")
    public String edit(@PathVariable Integer id,Model model){
        model.addAttribute("blog",mapper.selectBlogById(id));
        return "admin/blogs-inputs";
    }
    @GetMapping("/admin/blogs")
    public String GotoBlogs(Model model){
        model.addAttribute("blog",mapper.listBlog(1).getRecords());
        model.addAttribute("page",mapper.listBlog(1));
        model.addAttribute("wechat",gsdtService.getAllGsdt());
        return "admin/blogs";
    }
    @GetMapping("/admin/blogs/{id}/delete")
    public String deleteBlog(@PathVariable Integer id,RedirectAttributes attributes){
        int i = mapper.DeleteBlog(id);
        if(i>0){
            attributes.addAttribute("message","操作成功~");
            return "redirect:/admin/blogs";
        }
        else{
            attributes.addAttribute("message","操作失败");
            return "redirect:/admin/blogs";
        }
    }
    @GetMapping("/admin/search")
    public String easySearch(@PathVariable String title,Model model){
        model.addAttribute("blog",mapper.listBlogSearch(title));
        return "admin/blogs :: blogList";
    }
    @GetMapping("/admin/blogs/{list}")
    public String GotoBlogs(Model model,@PathVariable Integer list){
        model.addAttribute("blog",mapper.listBlog(list).getRecords());
        model.addAttribute("page",mapper.listBlog(list));
        model.addAttribute("wechat",gsdtService.getAllGsdt());
        return " admin/blogs::blogList";
    }
}
